<template>
  <div class="ChangePassword">
      <div class="header_title">
        <span>修改密码</span>
      </div>
      <el-row class="passwordmian">
        <el-col :span="10">
          <div class="grid-content bg-purple-dark">
                <el-form ref="form" :model="form" label-width="80px" class="password_form" :rules="passwordRule">
                    <el-form-item label="旧密码" prop="oldpassword">
                      <el-input v-model="form.oldpassword" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newpassword">
                       <el-input v-model="form.newpassword" show-password></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="confirmpassword">
                       <el-input v-model="form.confirmpassword" show-password></el-input>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" @click="onSubmit">确认修改</el-button>
                      <el-button @click="restpassword()">重置信息</el-button>
                    </el-form-item>
                </el-form>
          </div>
        </el-col>
      </el-row>
     
  </div>
</template>

<script>
import {JSEncrypt } from 'jsencrypt'
export default {
    data() {
      var validateConfirmPassword = (rule, value, callback) => {
        if (this.form.newpassword !== value) {
          callback(new Error('确认密码与新密码不一致'))
        } else {
          callback()
        }
      }
      return {
           publicKey:'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCx8wyZzYxaffWrJ4jooN554npe5vD2RSQGNXOY5UZz6hcIW0jdEk1KcdclE7mJPiI2DmgUjBp4Lau+bNmZxmLBqFHUMd29mWyqX+LuYVoymxs8f3VJF+7PqZzuKbtSOyXfcyjIIgtB06YlX4SXx4FCErGRx+MDseN2ObHSPme/3wIDAQAB',
           form: {
              oldpassword: '',
              newpassword: '',
              confirmpassword:'',
            },
            userGuid:'',
            passwordRule:{
              oldpassword:[
                 { required: true, message: '原密码不能为空', trigger: 'blur' }
              ],
              newpassword:[
                 { required: true, message: '新密码不能为空', trigger: 'blur' }
              ],
              confirmpassword:[
                  { required: true, message: '确认密码不能为空', trigger: 'blur' },
                  { validator: validateConfirmPassword, trigger: 'blur' }
              ],
            }
        }
    },
    created() {
      this.userGuid=sessionStorage.getItem('administrator')
    },
    methods: {
      onSubmit() {
          this.$refs['form'].validate((valid) => {
          if (valid) {
            let encryptor = new JSEncrypt();
                encryptor.setPublicKey(this.publicKey);
            let confirmPassword =  encryptor.encrypt(this.$md5(this.form.confirmpassword))
            let oldpassword =  encryptor.encrypt(this.$md5(this.form.oldpassword))
            this.$http({
                url: this.$http.adornUrl('/api/v1/change-admin-password'),
                method: 'post',
                data: this.$http.adornData({
                  messageContent:{
                      newPassword:confirmPassword,
                      oldPassword:oldpassword,
                      userGuid:this.userGuid
                  },
                  messageType:this.messageType
                })
                }).then(({data}) => {
                    console.log(data)
                  if (data.messageCode === 200) {
                    this.$message({
                      message: '修改密码成功',
                      type: 'success',
                    })
                    this.$router.push({path: 'login'})
                  } else {
                    this.$message.error(data.errorMessage)
                    this.init()
                  }
              })     
          }
        })
      },
      //重置密码
      restpassword(){
         this.$refs['form'].resetFields()
      }
    }
}
</script>

<style lang="scss" scoped>
.ChangePassword{
   padding: 10px;
   .header_title{
     height: 50px;
     line-height: 50px;
     border-bottom: 1px solid #e5e9f2;
     color:#909399;
   }
   .passwordmian{
     padding-top: 20px;
    
   }
}
</style>